<?php
/**
 * 前台通用函数
 */
/**
 * 返回处理好的导航结构数据
 * @param array $data
 */
function get_nav_menu($data)
{
    if (!is_array($data) || empty($data)) return [];
    foreach ($data as $k => &$v) {
        if ($v['type'] == 0) {
            // 如果栏目未填写路由，则默认按前台自己的控制器来，这里为article
            $Column = new \app\admin\model\Column();
            $cinfo  = $Column::where(['id' => $v['column_id']])->field(['id','route_name'])->find();
            if (empty($cinfo['route_name'])) {
                $v['url'] = url('/article/list_'.$cinfo['id']);
            } else {
                $v['url'] = url('/article/'.$cinfo['route_name']);
            }
        } elseif ($v['type'] == 1) {
            $Page  = new \app\admin\model\Page();
            $pinfo = $Page::where(['id' => $v['page_id']])->field(['id', 'route_name'])->find();
            if (empty($cinfo['route_name'])) {
                $v['url'] = url('/page/'.$pinfo['id']);
            } else {
                $v['url'] = url('/'.$pinfo['route_name']);
            }
        } elseif ($v['type'] == 2) {
            // 如果是外链，直接输出
            if (validate_url($v['url'])) {
                $v['url'] = $v['url'];
            } else {
                $v['url'] = url($v['url']);
            }
        }
    }

    return $data;
}

/**
 * 判断是否一个正确的URL地址
 */
function validate_url($url)
{
    $pattern_1 = "/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";
    $pattern_2 = "/^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i";       
    if (preg_match($pattern_1, $url) || preg_match($pattern_2, $url)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 查询父级的所有子级元素
 */
function get_all_subs($categorys, $catId = 0, $level = 1)
{
    $subs = array();
    foreach ($categorys as $item){
        if ($item['pid'] == $catId){
            $item['level'] = $level;
            $subs[]        = $item;
            $subs          = array_merge($subs, get_all_subs($categorys, $item['id'], $level + 1));
        } 
    }
    return $subs;
}


/**
 * 获取所有的父级分类
 * @param array $lists
 * @param integer|string $id
 * @return array
 */
function get_docs_parents($lists = [], $id = '')
{
    $trees = [];
    foreach ($lists as $value) {
        if ($value['id'] == $id) {
            $trees[] = $value;
            $trees   = array_merge(get_docs_parents($lists, $value['pid']), $trees);
        }
    }
    return $trees;
}

/**
 * 生成文档右侧菜单
 * @param array $tree
 * @param string $toute_name
 * @param array $location
 * @param integer $level
 * @return string
 */
function proc_html($tree, $route_name, $location, $level = 1)
{
    $html = ($level == 0) ? '<ul class="manual-menus">' : '<ul class="nav nav-subnav">';
    foreach($tree as $key => $value) {
        $is_active = $is_open = '';
        foreach ($location as $m => $n) {
            if ($key == $n['id']) {
                if (($m + 1) == count($location)) {
                    $is_active = ' active';
                }
                if (($m + 1) < count($location)) {
                    $is_open = ' open';
                }
            }
        }

        if(isset($value['_child']))
        {
            $html .= '<li class="nav-item nav-item-has-subnav'.$is_active.$is_open.'">';
            $html .= "<a href='".url('/manual/'.$route_name.'/'.$value['id'])."'>".$value['doc_name']."</a>";
            $html .= '<span class="caret"></span>';
            $html .= proc_html($value['_child'], $route_name, $location);
            $html .= "</li>";
        } else {
            $html .= "<li class='nav-item".$is_active."'><a href='".url('/manual/'.$route_name.'/'.$value['id'])."'>{$value['doc_name']}</a></li>";
        }
    }
    
    return $html."</ul>";
}

/**
 * 搜索标红
 * @param string $str 字符串
 * @param string $keywords 需要替换的关键词
 */
function replace_keywords($str, $keywords)
{
    return str_ireplace($keywords, '<span class="text-danger">'.$keywords.'</span>', $str);
}